package com.wmx.hb.controller;

import com.wmx.hb.enums.ResultCode;
import com.wmx.hb.mapper.Dept2Mapper;
import com.wmx.hb.pojo.Dept;
import com.wmx.hb.pojo.Emp;
import com.wmx.hb.pojo.ResultData;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author wangMaoXiong
 * @version 1.0
 * @date 2022/6/4 11:45
 */
@RestController
public class Dept2Controller {

    @Resource
    private Dept2Mapper dept2Mapper;

    /**
     * 根据部门编号查询部门信息
     * http://localhost:8080/dept2/loadDeptByNo/1
     *
     * @param deptno
     * @return
     */
    @GetMapping("/dept2/loadDeptByNo/{deptno}")
    public ResultData<Map> loadDeptByNo(@PathVariable("deptno") Integer deptno) {
        Map<String, Object> dept = dept2Mapper.loadDeptByNo(deptno);
        return new ResultData<>(ResultCode.SUCCESS, dept);
    }

    /**
     * 根据部门名称模糊查询
     * http://localhost:8080/dept2/loadDeptByName/sales
     *
     * @param dname
     * @return
     */
    @GetMapping("/dept2/loadDeptByName/{dname}")
    public ResultData<List> loadDeptByName(@PathVariable("dname") String dname) {
        List<Map<String, Object>> deptList = dept2Mapper.loadDeptByName(dname);
        return new ResultData<>(ResultCode.SUCCESS, deptList);
    }

    /**
     * 根据员工名称模糊查询
     * http://localhost:8080/dept2/loadEmpByName?ename=scott
     *
     * @param ename
     * @return
     */
    @GetMapping("/dept2/loadEmpByName")
    public ResultData<List> loadEmpByName(String ename) {
        List<Emp> deptList = dept2Mapper.loadEmpByName(ename);
        return new ResultData<>(ResultCode.SUCCESS, deptList);
    }

    /**
     * 根据部门编号查询员工信息
     * http://localhost:8080/dept2/loadEmpByDeptno/1
     *
     * @param deptno
     * @return
     */
    @GetMapping("/dept2/loadEmpByDeptno/{deptno}")
    public ResultData<List> loadEmpByDeptno(@PathVariable("deptno") Integer deptno) {
        List<Map<String, Object>> deptList = dept2Mapper.loadEmpByDeptno(deptno);
        return new ResultData<>(ResultCode.SUCCESS, deptList);
    }

    /**
     * 添加：  http://localhost:8080/dept2/add
     * {"dname":"Java开发部","loc":"长沙3"}
     *
     * @param dept
     * @return
     */
    @PostMapping("/dept2/add")
    public ResultData<String> addDept(@RequestBody Dept dept) {
        Integer execute = dept2Mapper.addDept(dept);
        ResultData<String> resultData = new ResultData<>(ResultCode.SUCCESS, execute + "," + dept.getDeptno());
        return resultData;
    }

    /**
     * 添加：  http://localhost:8080/dept2/add2
     * {"dname":"Java开发部","loc":"深圳"}
     *
     * @param dept
     * @return
     */
    @PostMapping("/dept2/add2")
    public ResultData<String> addDept2(@RequestBody Map<String, Object> dept) {
        Integer execute = dept2Mapper.addDept2(dept);
        ResultData<String> resultData = new ResultData<>(ResultCode.SUCCESS, execute + "," + dept.get("deptno"));
        return resultData;
    }

    /**
     * 更新部门信息
     * http://localhost:8080/dept2/updateDept?tableName=dept
     * {"loc":"上海","dname":"Java开发部","deptno":29}
     *
     * @param dept
     * @return
     */
    @PostMapping("/dept2/updateDept")
    public ResultData<String> updateDept(String tableName, @RequestBody Dept dept) {
        Integer execute = dept2Mapper.updateDept(tableName, dept);
        ResultData<String> resultData = new ResultData<>(ResultCode.SUCCESS, execute + "," + dept.getDeptno());
        return resultData;
    }

    /**
     * 更新部门信息
     * http://localhost:8080/dept2/updateDept2?tableName=dept
     * {"loc":"上海","dname":"Java开发部","deptno":24}
     *
     * @param dept
     * @return
     */
    @PostMapping("/dept2/updateDept2")
    public ResultData<String> updateDept(String tableName, @RequestBody Map<String, Object> dept) {
        Integer execute = dept2Mapper.updateDept2(tableName, dept);
        ResultData<String> resultData = new ResultData<>(ResultCode.SUCCESS, execute + "," + dept);
        return resultData;
    }

    /**
     * 根据主键删除部门信息
     * http://localhost:8080/dept2/deleteDept?tableName=dept
     * [27,28,29,30]
     *
     * @param tableName
     * @param deptnoList
     * @return
     */
    @PostMapping("/dept2/deleteDept")
    public ResultData<String> deleteDept(String tableName, @RequestBody List<String> deptnoList) {
        Integer execute = dept2Mapper.deleteDept(tableName, deptnoList);
        ResultData<String> resultData = new ResultData<>(ResultCode.SUCCESS, execute + "," + deptnoList);
        return resultData;
    }


}
